package com.example.orderservice.controller;

import com.example.orderservice.model.OrderInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.UUID;

@RequestMapping("/order")
@RestController
public class OrderController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/create")
    public String create() {
        // 下单相关业务逻辑实现...
        String orderId = UUID.randomUUID().toString();
        Random random = new Random();
        String orderName = "订单 " + random.nextInt(100);
        double price = random.nextDouble(200);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderId(orderId);
        orderInfo.setOrderName(orderName);
        orderInfo.setPrice(price);
        rabbitTemplate.convertAndSend("", "order.create", orderInfo);
        System.out.println("订单: " + orderInfo +" 发送成功");
        return "订单发送成功";
    }

    @RequestMapping("/createOrder")
    public String createOrder() {
        // 下单相关业务逻辑实现...
        // 区分不同的订单
        String orderId = UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend("", "order.create", "订单：" + orderId +" 发送成功");
        System.out.println("订单：" + orderId +" 发送成功");
        return "订单发送成功";
    }
}
